<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 *   Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */

require_once("entity.class.php");

class files extends entity {

	var $id;
	var $name;
	var $comment;
	var $statusid;
	var $userid;
	var $userid2;
	var $teamid;
	var $lastupdate;
	var $prjid;
	var $chstamp;
	
	var $prj;
	var $pre;
	var $url;
	
	/* STATS */
	var $translated;
	var $fuzzy;
	var $untranslated;
	var $filescount;
	
	var $order;
	var $orderDir;
	
	var $autoflag;
	var $record;
	var $totalrecord;
	var $lastreminder;
	
	var $st;
	
	var $fstatus = 0; // DELETED ?
	
	public static $orderFields;

    public function __construct($prj)
    {
    	$this->prj=PROJECT_PREFIX.$prj;
    	$this->pre=$prj;
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('files');	
		//return $class->newInstance();
		return new files($this->pre);
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function getIn($ids) {
		$inclause="";
		foreach ($ids as $id) {
			if ($inclause!="") {
				$inclause .= ",";
			}
			$inclause .= util::str($id);
		} 
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE id IN (".$inclause.")";
		return $sql;
	}
	
	public function findFile($pr) {
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE name = ".util::str($this->name). " AND teamid =".util::str($this->teamid);
		if (isset($pr)) {
			$sql .= " AND prjid = ".util::str($pr);
		}
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".FILES.$this->prj;
		return $sql;
	}

	public function workloadForUser() {
		$sql = "SELECT userid, count(id) as count, sum(translated) as translated, sum(fuzzy) as fuzzy, sum(untranslated) as untranslated FROM ".FILES.$this->prj." WHERE userid=".util::str($this->userid)." AND (statusid=".util::str(FILE_STATUS_LOCKED)." OR statusid=".util::str(FILE_STATUS_PROOF).") AND fstatus = '0' AND teamid=".util::str($this->teamid)." GROUP BY userid";
		return $sql;
	}

	public function workload() {
		$sql = "SELECT userid, count(id) as count, sum(translated) as translated, sum(fuzzy) as fuzzy, sum(untranslated) as untranslated FROM ".FILES.$this->prj." WHERE (statusid=".util::str(FILE_STATUS_LOCKED)." OR statusid=".util::str(FILE_STATUS_PROOF).") AND fstatus = '0' AND  teamid=".util::str($this->teamid)." GROUP BY userid";
		return $sql;
	}
	
	public function loadAssList() {
		$sql = "SELECT * FROM ".FILES.$this->prj." WHERE fstatus = '0' AND teamid = ".util::str($this->teamid)." AND ((userid IS NOT NULL AND userid!='') OR (userid2 IS NOT NULL AND userid2!=''))";
		return $sql;
	}

	public function loadfiltered($count = false,$byuser = null) {
		if ($count!==true) {
			$sql = "SELECT f.*,p.name as prjname FROM ".FILES.$this->prj." f INNER JOIN ".PROJECTS." p ON f.prjid = p.id";	
		} else {
			$sql = "SELECT count(f.id) FROM ".FILES.$this->prj." f";
		}
		$filtered = "";
		if ($this->statusid != null) {
			if (count($this->statusid)>1) {
				$filtered .= " (f.statusid = ".util::str($this->statusid[0]);
				for ($i=1;$i<count($this->statusid);$i++) {
					$filtered .= " OR f.statusid = ".util::str($this->statusid[$i]);
				}
				$filtered.=")";
			} else {
				$filtered .= " f.statusid = ".util::str($this->statusid);	
			}
		}
		if ($this->userid != null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " f.userid LIKE ".util::str($this->userid."%");
		}
		if ($this->userid2 != null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " f.userid2 LIKE ".util::str($this->userid2."%");
		}		
		if ($this->name != null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " f.name LIKE ".util::str($this->name."%");
		}	
		if ($this->prjid != null) {
			if ($filtered != "") $filtered .= " AND ";
			if (count($this->prjid)>1) {
				$filtered .= " (f.prjid = ".util::str($this->prjid[0]);
				for ($i=1;$i<count($this->prjid);$i++) {
					$filtered .= " OR f.prjid = ".util::str($this->prjid[$i]);
				}
				$filtered.=")";
			} else {
				$filtered .= " f.prjid = ".util::str($this->prjid);	
			}
		}
		if ($this->teamid != null) {
			if ($filtered != "") $filtered .= " AND ";
			$filtered .= " f.teamid = ".util::str($this->teamid);
		}
		if ($this->st != "") {
			switch ( $this->st) {
				case 1:
					if ($filtered != "") $filtered .= " AND ";
					$filtered .= " (f.fuzzy=0 and f.untranslated=0)";
					break;
				case 2:
					if ($filtered != "") $filtered .= " AND ";
					$filtered .= " (f.fuzzy>0 and f.untranslated=0)";
					break;		
				case 3:
					if ($filtered != "") $filtered .= " AND ";
					$filtered .= " f.untranslated>0";
					break;		
				case 4:
					if ($filtered != "") $filtered .= " AND ";
					$filtered .= " (f.fuzzy>0 OR f.untranslated>0)";
					break;		
			}
		}
		if ($byuser!=null) {
			if ($filtered != "") $filtered .= " AND ";			
			$filtered .= " (f.userid = ".util::str($byuser)." OR f.userid2 = ".util::str($byuser).")";			
		}
		if ($filtered != "") $sql .= " WHERE fstatus = '0' AND " .$filtered;
		if ((isset($this->order) and $this->order!="") and $count!==true) {
			$sql .= " ORDER BY f.".$this->order." ".($this->orderDir != "" ? $this->orderDir : "ASC");
		}
		if ($count!==true) {
			if ($this->totalrecord!=null) {
				$sql .= " LIMIT ".$this->record.",".$this->totalrecord;
			}
		}
		return $sql;
	}
	
	public function countTeam() {
		$sql = "SELECT count(id) FROM ".FILES.$this->prj." WHERE fstatus = '0' AND teamid = ".util::str($this->teamid);
		return $sql;
	}
	
	public function listMyFiles() {
		$sql = "SELECT f.* FROM ".FILES.$this->prj." f, ".HISTORY.$this->prj." h WHERE " .
				"h.userid = ".util::str($this->userid)." AND h.statusid = ".util::str($this->statusid).
				" AND f.id = h.fileid GROUP BY f.id ORDER BY f.name"; 
		return $sql;
	}
	
	public function insert() {
      $sql = "INSERT INTO ".FILES.$this->prj." (name, statusid, teamid, prjid) ";
      $sql .= "VALUES (".util::str($this->name).",";
      $sql .= util::str(FILE_STATUS_TODO).",".util::str($this->teamid).",".util::str($this->prjid).")";
	  return $sql;
	}

	public function getStats() {
		$sql = "SELECT sum(translated) as translated, sum(untranslated) as untranslated, sum(fuzzy) as fuzzy FROM ".FILES.$this->prj." where teamid=".util::str($this->teamid)." and fstatus = '0'";
		return $sql;
	}
	
	public function update($date=true,$stats=false) {
	  $dt = util::now();
	  $this->lastupdate = util::mySqlDateFormat($dt);
	  $this->chstamp = util::hashcode($this->id);
	  $this->comment = util::encode(removeBreakLine($this->comment));
      $sql = "UPDATE ".FILES.$this->prj;
      $sql .= " set statusid = ".util::str($this->statusid);
      if ($date) $sql .= ", lastupdate = ".util::str($dt);
      $sql .= ", userid = ".util::str($this->userid);
      $sql .= ", userid2 = ".util::str($this->userid2);
      $sql .= ", comment = ".util::str($this->comment);
      $sql .= ", chstamp = ".util::str($this->chstamp);
      $sql .= ", autoflag = ".util::str($this->autoflag);
      $sql .= ", fstatus = ".util::str($this->fstatus);
      if ($stats) {
      $sql .= ", translated = ".util::str($this->translated);
      $sql .= ", fuzzy = ".util::str($this->fuzzy);
      $sql .= ", untranslated = ".util::str($this->untranslated);
      }
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}

	public function stats() {
      $sql = "UPDATE ".FILES.$this->prj;
      $sql .= " set translated = ".util::str($this->translated);
      $sql .= ", fuzzy = ".util::str($this->fuzzy);
      $sql .= ", untranslated = ".util::str($this->untranslated);
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}

	public function reminder() {
      $sql = "UPDATE ".FILES.$this->prj;
      $sql .= " set lastreminder = ".util::str(util::now());
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}
	
	public function deleteTeam($prjid,$teamid) {
		$sql = "DELETE FROM ".FILES.$this->prj;
		$sql .= " WHERE teamid = ".util::str($teamid);
		$sql .= " AND prjid = ".util::str($prjid);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".FILES.$this->prj;
		$sql .= " WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function total() {
		if ($this->untranslated=="" or $this->fuzzy=="" or $this->translated=="") {
			return "";
		}
		return $this->untranslated+$this->fuzzy+$this->translated;
	}

	public function state() {
		if ($this->untranslated=="" or $this->fuzzy=="") {
			return 0;
		} elseif ($this->untranslated>0) {
			return 3;
		} elseif ($this->fuzzy>0) {
			return 2;
		} else {
			return 1;
		}
	}		

	public function fill($array) {
		$this->id=$array['id'];
		$this->name=$array['name'];
		$this->comment=util::decode($array['comment']);
		$this->statusid= $array['statusid'];
		$this->teamid=$array['teamid'];
		$this->userid=$array['userid'];
		$this->userid2=$array['userid2'];
		$this->lastupdate=$array['lastupdate'];
		$this->prjid=$array['prjid'];
		$this->chstamp=$array['chstamp'];
		$this->translated=$array['translated'];
		$this->fuzzy=$array['fuzzy'];
		$this->untranslated=$array['untranslated'];
		$this->autoflag=$array['autoflag'];
		$this->filescount=$array['count'];
		$this->fstatus=$array['fstatus'];
		$this->lastreminder=$array['lastreminder'];
		if (isset($array['prjname'])) {
			$this->prjname=$array['prjname'];
		}
	}
			
}

?>
